import { ref } from 'vue'
import { defineStore } from 'pinia'

export const usePostListStore = defineStore('postList', () => {
    const postList = ref(JSON.parse(localStorage.getItem('postList')) || [])
    const topicList = ref(['话题1', '话题2', '话题3', '话题4'])

    function setPostList() {
        localStorage.setItem('postList', JSON.stringify(postList.value))
    }

    function addPost(post) {
        postList.value.unshift(post)
        setPostList()
    }

    function delPost(postid) {
        for (let i = 0; i < postList.value.length; i++) {
            if (postList.value[i].postid == postid) {
                postList.value.splice(i, 1)
                break
            }
        }
        setPostList()
    }

    function getPost(postid) {
        for (let i = 0; i < postList.value.length; i++) {
            if (postList.value[i].postid == postid) {
                return postList.value[i]
            }
        }
    }

    function getUserPosts(userid) {
        let arr = []
        for (let i = 0; i < postList.value.length; i++) {
            if (postList.value[i].userid == userid) {
                arr.push(postList.value[i])
            }
        }
        return arr
    }

    function getTopicPost(topic) {
        let posts = []
        for (let i = 0; i < postList.value.length; i++) {
            if (postList.value[i].topics.indexOf(topic) != -1) {
                posts.push(postList.value[i])
            }
        }
        return posts
    }

    function addComment(data) {
        for (let i = 0; i < postList.value.length; i++) {
            if (postList.value[i].postid == data.postid) {
                postList.value[i].comments.unshift(data.data)
                break
            }
        }
        setPostList()
    }

    function getCommnet(data) {
        for (let i = 0; i < postList.value.length; i++) {
            if (postList.value[i].postid == data.postid) {
                for (let j = 0; j < postList.value[i].comments.length; j++) {
                    if (postList.value[i].comments[j].commentid == data.commentid) {
                        return postList.value[i].comments[j]
                    }
                }
            }
        }
    }

    function addlike(data) {
        for (let i = 0; i < postList.value.length; i++) {
            if (postList.value[i].postid == data.postid) {
                if (postList.value[i].likes.indexOf(data.userid) == -1) {
                    postList.value[i].likes.push(data.userid)
                    setPostList()
                    return true
                }
            }
        }
    }

    function addCollect(data) {
        for (let i = 0; i < postList.value.length; i++) {
            if (postList.value[i].postid == data.postid) {
                if (postList.value[i].collects.indexOf(data.userid) == -1) {
                    postList.value[i].collects.push(data.userid)
                    setPostList()
                    return true
                }
            }
        }
    }

    return {
        postList,
        topicList,
        addPost,
        delPost,
        getUserPosts,
        getTopicPost,
        getPost,
        addComment,
        getCommnet,
        addlike,
        addCollect,
    }
})
